package hczc.longcai.com.hccrowdfunding.home.fourth_mine.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;

/**
 * Created by Administrator on 2016/1/28.
 */
public class AddressAdapter extends BaseAdapter implements View.OnClickListener {

    private Context context;
    private List<Address> list;
    private int state;
    private CallBack callBack;


    public AddressAdapter(Context context, List<Address> list, int state, CallBack callBack) {
        this.context = context;
        this.list = list;
        this.state = state;
        this.callBack = callBack;
    }

    public AddressAdapter(Context context, List<Address> list, CallBack callBack) {
        this.context = context;
        this.list = list;
        this.callBack = callBack;
    }

    public AddressAdapter(Context context, List<Address> list, int state) {
        this.context = context;
        this.list = list;
        this.state = state;
    }

    public AddressAdapter(Context context, List<Address> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            holder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_address_manage,null);
            BaseApplication.scaleScreenHelper.loadView(((ViewGroup) convertView));
            holder.name = ((TextView) convertView.findViewById(R.id.address_manage_name));
            holder.phone = ((TextView) convertView.findViewById(R.id.address_manage_phone));
            holder.address = ((TextView) convertView.findViewById(R.id.address_manage_address));
            holder.choose = ((ImageView) convertView.findViewById(R.id.address_manage_choose));
            holder.enter = ((ImageView) convertView.findViewById(R.id.address_manage_enter));
            convertView.setTag(holder);
        }
        if(state == 0) {
            Address address = list.get(position);
            holder = ((ViewHolder) convertView.getTag());
            holder.name.setText(address.getName());
            holder.phone.setText(address.getPhone());
            holder.address.setText(address.getArea()+address.getAddress());
            holder.enter.setVisibility(View.GONE);
            holder.choose.setVisibility(View.VISIBLE);
            holder.choose.setOnClickListener(this);
            holder.choose.setTag(position);
            if(address.isDfault()){
                holder.choose.setSelected(true);
            }else {
                holder.choose.setSelected(false);
            }
        }else if(state == 1){
            Address address = list.get(position);
            holder = ((ViewHolder) convertView.getTag());
            holder.name.setText(address.getName());
            holder.phone.setText(address.getPhone());
            holder.address.setText(address.getArea()+address.getAddress());
            holder.choose.setVisibility(View.GONE);
            holder.enter.setVisibility(View.VISIBLE);
        }

        return convertView;
    }

    public interface CallBack{
        void click(View view);
    }

    @Override
    public void onClick(View v) {
        callBack.click(v);
    }

    class ViewHolder{
        TextView name,phone,address;
        ImageView choose,enter;

    }
}
